package com.cskaoyan.service.wx.collect;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.wx.CollectAddOrDeleteBo;
import com.cskaoyan.bean.vo.wx.collect.CollectListVo;
import com.cskaoyan.bean.vo.wx.collect.CollectListVo2;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/09/22:15
 * @Description:
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
    * @Author: 宾文杰 @Description: 显示用户全部收藏信息 @Date: 2022/9/9
    */
    @Override
    public CollectListVo collectList(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = null;
        try {
            user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            e.printStackTrace();
        }
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(type.byteValue());
        criteria.andUserIdEqualTo(user.getId());
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        PageInfo info = new PageInfo(marketCollects);
        ArrayList<CollectListVo2> collectListVo2s = new ArrayList<>();
        for (MarketCollect marketCollect : marketCollects) {
            CollectListVo2 collectListVo2 = new CollectListVo2();
            Integer goodsId = marketCollect.getValueId();
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            collectListVo2.setBrief(marketGoods.getBrief());
            collectListVo2.setId(marketCollect.getId());
            collectListVo2.setName(marketGoods.getName());
            collectListVo2.setPicUrl(marketGoods.getPicUrl());
            collectListVo2.setRetailPrice(marketGoods.getRetailPrice());
            collectListVo2.setType(marketCollect.getType());
            collectListVo2.setValueId(goodsId);
            collectListVo2s.add(collectListVo2);
        }
        CollectListVo collectListVo = new CollectListVo();
            collectListVo.setList(collectListVo2s.toArray());
            collectListVo.setPage(page);
            collectListVo.setPages(info.getPages());
            collectListVo.setTotal( info.getTotal());
        collectListVo.setLimit(limit);
        return collectListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 新增或删除收藏信息 @Date: 2022/9/9
    */
    @Override
    public void addOrDelete(CollectAddOrDeleteBo addOrDeleteBo) {
        Integer type = addOrDeleteBo.getType();
        Integer valueId = addOrDeleteBo.getValueId();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(type.byteValue());
        criteria.andValueIdEqualTo(valueId);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        if (marketCollects != null&&marketCollects.size()!=0) {
            collectMapper.deleteByExample(marketCollectExample);
        } else {
            MarketCollect marketCollect = new MarketCollect();
            //获取当前用户信息
            Subject subject = SecurityUtils.getSubject();
            MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            marketCollect.setUserId(principal.getId());
            marketCollect.setType(type.byteValue());
            marketCollect.setValueId(valueId);
            marketCollect.setAddTime(new Date());
            marketCollect.setUpdateTime(new Date());
            collectMapper.insertSelective(marketCollect);
        }

    }
}
